package com.skyworthclub.competition.config.security.handler;

import com.alibaba.fastjson.JSON;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author li
 * @version 1.0
 * @since 18-10-16 下午3:58
 * 成功退出登录后的处理器
 **/
@Component
public class CustomLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //ajax请求
//        if (MediaType.APPLICATION_JSON_VALUE.equals(request.getContentType())
//                || MediaType.APPLICATION_JSON_UTF8_VALUE.equals(request.getContentType())) {
            Map<String, Object> map = new HashMap<>();
            map.put("status", 200);
            map.put("message", "注销成功！");

        response.setContentType("application/json");
        response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().write(JSON.toJSONString(map));
            response.getWriter().flush();
//        } else {
////            StringBuffer requestURL = request.getRequestURL();
//            response.sendRedirect("/");
//        }

    }
}
